package com.zhengshang.web.controller.app;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.zhengshang.api.domain.*;
import com.zhengshang.api.service.IAppBillRefundRecordService;
import com.zhengshang.api.service.IAppPhoneAreaService;
import com.zhengshang.common.annotation.RepeatSubmit;
import com.zhengshang.common.constant.app.AppConstant;
import com.zhengshang.common.enums.UserStatus;
import com.zhengshang.common.pay.alipay.AlipayService;
import com.zhengshang.common.utils.StringUtils;
import com.zhengshang.third.phone.PhoneAreaUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.compress.utils.Lists;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zhengshang.common.annotation.Log;
import com.zhengshang.common.core.controller.BaseController;
import com.zhengshang.common.core.domain.AjaxResult;
import com.zhengshang.common.enums.BusinessType;
import com.zhengshang.api.service.IAppPayRecordService;
import com.zhengshang.common.utils.poi.ExcelUtil;
import com.zhengshang.common.core.page.TableDataInfo;

/**
 * APP交易记录Controller
 *
 * @author zhengshang
 * @date 2021-06-21
 */
@RestController
@RequestMapping("/app/payRecord")
public class AppPayRecordController extends BaseController {
    @Autowired
    private IAppPayRecordService appPayRecordService;
    @Autowired
    private IAppPhoneAreaService phoneAreaService;

    /**
     * 查询APP交易记录列表
     */
    @PreAuthorize("@ss.hasPermi('app:payRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppPayRecord appPayRecord) {
        String buyType = appPayRecord.getBuyType();
        startPage();
        appPayRecord.setStatus("20");
        appPayRecord.setDelFlag(UserStatus.OK.getCode());
        //油卡列表
        if (AppConstant.ORDERNUM_PREFIX_OILCARD.equals(buyType)) {
            List<OilcardAppPayRecord> list = appPayRecordService.queryOilcardOrderby(appPayRecord);
            return getDataTable(list);
        } else if (AppConstant.ORDERNUM_PREFIX_THIRD.equals(buyType)) {
            //影视订单列表
            List<ThirdCloumnAppPayRecord> list = appPayRecordService.queryThirdColumnOrderby(appPayRecord);
            return getDataTable(list);
        } else if (AppConstant.ORDERNUM_PREFIX_BILL.equals(buyType)) {
            //话费订单
            List<AppPayRecord> list = appPayRecordService.queryListOrderby(appPayRecord);
            list.forEach(payRecord -> {
                String phone = payRecord.getPhone();
                AppPhoneArea phoneInfo = phoneAreaService.queryByPhone(phone);
                if (StringUtils.isNotNull(phoneInfo)) {
                    String phoneArea = phoneInfo.getPhoneArea();
                    String phoneNewIsp = phoneInfo.getPhoneNewIsp();
                    payRecord.setPhone(phoneArea + "|" + phoneNewIsp + "|" + phone);
                }
            });
            return getDataTable(list);
        } else {
            List<AppPayRecord> list = appPayRecordService.queryListOrderby(appPayRecord);
            return getDataTable(list);
        }
    }

    /**
     * 导出APP交易记录列表
     */
    @PreAuthorize("@ss.hasPermi('app:payRecord:export')")
    @Log(title = "APP交易记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AppPayRecord appPayRecord) {
        //appPayRecord.setStatus("20");
        appPayRecord.setDelFlag(UserStatus.OK.getCode());
        String buyType = appPayRecord.getBuyType();
        //油卡列表
        if (AppConstant.ORDERNUM_PREFIX_OILCARD.equals(buyType)) {
            List<OilcardAppPayRecord> list = appPayRecordService.queryOilcardOrderby(appPayRecord);
            ExcelUtil<OilcardAppPayRecord> util = new ExcelUtil<>(OilcardAppPayRecord.class);
            return util.exportExcel(list, "pay");
        }
        //影视订单列表
        else if (AppConstant.ORDERNUM_PREFIX_THIRD.equals(buyType)) {
            List<ThirdCloumnAppPayRecord> list = appPayRecordService.queryThirdColumnOrderby(appPayRecord);
            ExcelUtil<ThirdCloumnAppPayRecord> util = new ExcelUtil<>(ThirdCloumnAppPayRecord.class);
            return util.exportExcel(list, "pay");
        } else {
            List<AppPayRecord> list = appPayRecordService.queryListOrderby(appPayRecord);
            if (AppConstant.ORDERNUM_PREFIX_BILL.equals(buyType)) {
                list.forEach(payRecord -> {
                    String phone = payRecord.getPhone();
                    AppPhoneArea phoneInfo = phoneAreaService.queryByPhone(phone);
                    if (StringUtils.isNotNull(phoneInfo)) {
                        String phoneArea = phoneInfo.getPhoneArea();
                        String phoneNewIsp = phoneInfo.getPhoneNewIsp();
                        payRecord.setPhone(phoneArea + "|" + phoneNewIsp + "|" + phone);
                    }
                });
            }
            ExcelUtil<AppPayRecord> util = new ExcelUtil<>(AppPayRecord.class);
            return util.exportExcel(list, "pay");
        }
    }

    /**
     * 获取APP交易记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('app:payRecord:query')")
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId) {
        return AjaxResult.success(appPayRecordService.selectAppPayRecordById(recordId));
    }


    /**
     * @title 更新状态
     */
    @ApiOperation(value = "更新状态")
    @RepeatSubmit
    @PostMapping("/updateRechargeState")
    public AjaxResult updateRechargeState(@RequestBody AppPayRecord appPayRecord) {
        return AjaxResult.success(appPayRecordService.updateAppPayRecord2(appPayRecord));
    }


    /**
     * @title 充值退款
     */
    @ApiOperation(value = "充值退款")
    @RepeatSubmit
    @PostMapping("/refund")
    public AjaxResult refund(@RequestBody AppPayRecord appPayRecord) {
        return toAjax(appPayRecordService.refund(appPayRecord));
    }
}
